package com.mycompany.baoming.controller;

import com.mycompany.baoming.entity.*;
import com.mycompany.baoming.service.BaomingService;
import com.mycompany.mvc.utils.EasyResult;
import com.mycompany.mvc.utils.RequestUtil;
import com.mycompany.mvc.utils.SystemUtil;
import com.mycompany.user.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Jintao_Ma on 2017/9/16.
 */
@Controller
@RequestMapping("/bm")
public class BaomingController {

    Logger logger = LoggerFactory.getLogger(BaomingController.class);

    @Autowired
    private BaomingService baomingService;

    /**
     * 用户报名信息菜单跳转
     * @param
     * @return
     */
    @RequestMapping("/getUserBm")
    @ResponseBody
    public ModelAndView getUserBm(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        ModelAndView mv = new ModelAndView("/system/user/userList");
        mv.addObject("user",user);
        return mv;
    }

    /**
     * 用户报名信息菜单List
     * @param
     * @return
     */
    @RequestMapping("/getUserBmList")
    @ResponseBody
    public EasyResult getUserBmList(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Integer userId = user.getUserId();
        if(SystemUtil.isNull(userId)){
            return new EasyResult();
        }
        Map<String, Object> paraMap = RequestUtil.getReqest(request);
        paraMap.put("userId",userId);
        return baomingService.getUserBmList(paraMap);
    }

    /**
     * 用户报名信息菜单List
     * @param
     * @return
     */
    @RequestMapping("/getUserBmListOr")
    @ResponseBody
    public EasyResult getUserBmListOr(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Integer userId = user.getUserId();
        if(SystemUtil.isNull(userId)){
            return new EasyResult();
        }
        Map<String, Object> paraMap = new HashMap<>();
        paraMap.put("userId",userId);
        return baomingService.getUserBmListOr(paraMap);
    }

    /**
     * 用户报名信息菜单编辑页
     * @param
     * @return
     */
    @RequestMapping("/UserBmToSave")
    @ResponseBody
    public ModelAndView UserBmToSave(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        List<SexDic> sexDic = BmDicConstant.getSexDic();
        List<CansaiDic> ItemDic = BmDicConstant.getCansaiDic();
        List<ProDic> ProList = BmDicConstant.getProDic();
        List<ProDic> ProListW5 = BmDicConstant.getPro5Dic();
        List<ProDic> ProListW6 = BmDicConstant.getPro6Dic();
        List<ProDic> ProListWA = BmDicConstant.getProWDic();
        List<ProDic> TeamList = BmDicConstant.getTeamDic();
        List<ProDic> CloDic = BmDicConstant.getCloDic();

        ModelAndView mv = new ModelAndView("/system/user/userSave");
        mv.addObject("userId",user.getUserId());
        mv.addObject("user",user);
        mv.addObject("ItemDic",ItemDic);
        mv.addObject("ProList",ProList);
        mv.addObject("ProListW5",ProListW5);
        mv.addObject("ProListW6",ProListW6);
        mv.addObject("ProListWA",ProListWA);
        mv.addObject("TeamList",TeamList);
        mv.addObject("CloDic",CloDic);
        mv.addObject("sexDic",sexDic);
        return mv;
    }

    /**
     * 用户报名信息菜单编辑页
     * @param
     * @return
     */
    @RequestMapping(value="/UserBmSave",method = RequestMethod.POST)
    @ResponseBody
    public EasyResult UserBmSave(HttpServletRequest request){
        return baomingService.UserBmSave(request);
    }

    /**
     * 用户报名信息菜单编辑页
     * @param
     * @return
     */
    @RequestMapping("/UserBmToEdit")
    @ResponseBody
    public ModelAndView toEdit(@RequestParam(value = "id", required = true) int id){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        return baomingService.UserBmToEdit(map);
    }

    /**
     * 用户报名信息菜单编辑页
     * @param
     * @return
     */
    @RequestMapping("/UserBmDelete")
    @ResponseBody
    public EasyResult UserBmDelete(@RequestParam(value = "id", required = true) int id){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        return baomingService.UserBmDelete(map);
    }

    /**
     * 用户报名信息菜单提交
     * @param
     * @return
     */
    @RequestMapping("/UserBmEdit")
    @ResponseBody
    public EasyResult UserBmEdit(HttpServletRequest request){
        return baomingService.UserBmEdit(request);
    }

    /*************************************************[组织]**********************************************************/
    /**
     * 组织报名信息菜单跳转
     * @param
     * @return
     */
    @RequestMapping("/getOrgBm")
    @ResponseBody
    public ModelAndView getOrgBm(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        ModelAndView mv = new ModelAndView("/system/org/orgList");
        mv.addObject("user",user);
        return mv;
    }

    /**
     * 用户报名信息菜单List
     * @param
     * @return
     */
    @RequestMapping("/getOrgBmList")
    @ResponseBody
    public EasyResult getOrgBmList(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Integer userId = user.getUserId();
        if(SystemUtil.isNull(userId)){
            return new EasyResult();
        }
        Map<String, Object> paraMap = RequestUtil.getReqest(request);
        paraMap.put("userId",userId);
        return baomingService.getUserBmList(paraMap);
    }

    /**
     * 用户报名信息菜单编辑页
     * @param
     * @return
     */
    @RequestMapping("/OrgBmToSave")
    @ResponseBody
    public ModelAndView OrgBmToSave(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        List<SexDic> sexDic = BmDicConstant.getSexDic();
        List<CansaiDic> ItemDic = BmDicConstant.getCansaiDic();
        List<ProDic> ProList = BmDicConstant.getProDic();
        List<ProDic> ProListW5 = BmDicConstant.getPro5Dic();
        List<ProDic> ProListW6 = BmDicConstant.getPro6Dic();
        List<ProDic> ProListWA = BmDicConstant.getProWDic();
        List<ProDic> TeamList = BmDicConstant.getTeamDic();
        List<ProDic> CloDic = BmDicConstant.getCloDic();

        ModelAndView mv = new ModelAndView("/system/org/orgSave");
        mv.addObject("userId",user.getUserId());
        mv.addObject("user",user);
        mv.addObject("ItemDic",ItemDic);
        mv.addObject("ProList",ProList);
        mv.addObject("ProListW5",ProListW5);
        mv.addObject("ProListW6",ProListW6);
        mv.addObject("ProListWA",ProListWA);
        mv.addObject("TeamList",TeamList);
        mv.addObject("CloDic",CloDic);
        mv.addObject("sexDic",sexDic);
        return mv;
    }

    /**
     * 用户报名信息菜单编辑页
     * @param
     * @return
     */
    @RequestMapping(value="/OrgBmSave",method = RequestMethod.POST)
    @ResponseBody
    public EasyResult OrgBmSave(HttpServletRequest request){
        return baomingService.UserBmSave(request);
    }

    /**
     * 用户报名信息菜单编辑页
     * @param
     * @return
     */
    @RequestMapping("/OrgBmToEdit")
    @ResponseBody
    public ModelAndView OrgBmToEdit(@RequestParam(value = "id", required = true) int id){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        return baomingService.OrgBmToEdit(map);
    }

    /**
     * 用户报名信息菜单编辑页
     * @param
     * @return
     */
    @RequestMapping("/OrgBmDelete")
    @ResponseBody
    public EasyResult OrgBmDelete(@RequestParam(value = "id", required = true) int id){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        return baomingService.UserBmDelete(map);
    }

    /**
     * 用户报名信息菜单提交
     * @param
     * @return
     */
    @RequestMapping("/OrgBmEdit")
    @ResponseBody
    public EasyResult OrgBmEdit(HttpServletRequest request){
        return baomingService.UserBmEdit(request);
    }

    /*************************************************[管理员]**********************************************************/
    /**
     * 组织报名信息菜单跳转
     * @param
     * @return
     */
    @RequestMapping("/getUserSh")
    @ResponseBody
    public ModelAndView getUserSh(HttpServletRequest request){
        String type = request.getParameter("type");
        //3查看个人运动员
        //2查看组织运动员
        //1查看所有运动员
        User user = (User) request.getSession().getAttribute("user");
        ModelAndView mv = new ModelAndView("system/admin/adminList");
        mv.addObject("user",user);
        mv.addObject("type",type);
        return mv;
    }

    /**
     * 用户报名信息菜单List
     * @param
     * @return
     */
    @RequestMapping("/getAdminBmList")
    @ResponseBody
    public EasyResult getAdminBmList(HttpServletRequest request){
//        HttpSession session = request.getSession();
//        User user = (User) session.getAttribute("user");
//        Integer userId = user.getUserId();
//        if(SystemUtil.isNull(userId)){
//            return new EasyResult();
//        }
        Map<String, Object> paraMap = RequestUtil.getReqest(request);
        //为了个人  组织,否则查询所有
        String Role = request.getParameter("Role");
        if(StringUtils.equals("3",Role) || StringUtils.equals("2",Role)){
            paraMap.put("Role",Role);
        }else{
            paraMap.remove("Role");
        }
        //为了九个参赛项目小菜单
        String bsInfos = request.getParameter("bsInfos");
        paraMap.put("bsInfos",bsInfos);

        return baomingService.getUserBmList(paraMap);
    }

    /**
     * 用户报名信息菜单新增页
     * @param
     * @return
     */
    @RequestMapping("/AdminBmToSave")
    @ResponseBody
    public ModelAndView AdminBmToSave(HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        List<SexDic> sexDic = BmDicConstant.getSexDic();
        List<CansaiDic> ItemDic = BmDicConstant.getCansaiDic();
        ModelAndView mv = new ModelAndView("system/admin/adminSave");
        mv.addObject("userId",user.getUserId());
        mv.addObject("user",user);
        mv.addObject("ItemDic",ItemDic);
        mv.addObject("sexDic",sexDic);
        return mv;
    }

    /**
     * 用户报名信息菜单保存页
     * @param
     * @return
     */
    @RequestMapping(value="/AdminBmSave",method = RequestMethod.POST)
    @ResponseBody
    public EasyResult AdminBmSave(HttpServletRequest request){
        return baomingService.UserBmSave(request);
    }

    /**
     * 用户报名信息菜单编辑页
     * @param
     * @return
     */
    @RequestMapping("/AdminBmToEdit")
    @ResponseBody
    public ModelAndView AdminBmToEdit(@RequestParam(value = "id", required = true) int id){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        return baomingService.OrgBmToEdit(map);
    }

    /**
     * 用户报名信息菜单编辑页
     * @param
     * @return
     */
    @RequestMapping("/AdminBmDelete")
    @ResponseBody
    public EasyResult AdminBmDelete(@RequestParam(value = "id", required = true) int id){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        return baomingService.UserBmDelete(map);
    }

    /**
     * 用户报名信息菜单提交
     * @param
     * @return
     */
    @RequestMapping("/AdminBmEdit")
    @ResponseBody
    public EasyResult AdminBmEdit(HttpServletRequest request){
        return baomingService.UserBmEdit(request);
    }

    /*************************************************[管理员 参赛分类]*************************************************/
    /**
     * 组织报名信息菜单跳转
     * @param
     * @return
     */
    @RequestMapping("/cansai")
    @ResponseBody
    public ModelAndView cansai(HttpServletRequest request){
        String type = request.getParameter("type");
        //3查看个人运动员
        //2查看组织运动员
        //1查看所有运动员
        User user = (User) request.getSession().getAttribute("user");
        ModelAndView mv = new ModelAndView("system/admin/cansaiList");
        mv.addObject("user",user);
        mv.addObject("type",type);
        return mv;
    }


}
